/*
 * Edit In Place
 * http://josephscott.org/code/js/eip/
 *
 * Version: 0.2.2
 * License: http://josephscott.org/code/js/eip/license.txt
 */

/* 
 * 20091008: Modified by sonofdelphi for Neptuner. 
 * Change summary follows:
 *
 * Removed innerHTML
 * Activation on double-click instead of single-click
 * Adds a new element instead of editing in place
 * 
 */

InPlaceNote = function() { };
InPlaceNote.settings = function(set) {
	var settings = {
		id:			false,
		save_url:		false,
		css_class:		'ipn_editable',
		savebutton:		'ipn_savebutton',
		cancelbutton:		'ipn_cancelbutton',
		saving:			'ipn_saving',
		empty:			'ipn_empty',
		type:			'text',
		is_empty:		false,
		empty_text:		'Double-Click To Add Content',
		orig_text:		false,
		options:		{ },
		option_name:	false
	};

	for(var i in set) { settings[i] = set[i]; }

	return($(settings));
};

InPlaceNote.formField = function(set) {
	var field = '';

	set['orig_text'] = 'Enter comment.';

	if(set['type'] == 'textarea') {
		var cols = 50;
		if(set['cols']) { cols = set['cols']; }
		var rows = (set['orig_text'].length / cols) + 3;
		if(set['rows']) { rows = set['rows']; }

		field = '<span id="' + set['id'] + '_editor"><textarea id="'
			+ set['id'] + '_edit" class="' + set['css_class'] + '" name="'
			+ set['id'] + '_edit" rows="' + rows + '" cols="'
			+ cols + '">' + set['orig_text'] + '</textarea><br />';
	}

	return(field);
};

InPlaceNote.formButtons = function(set) {
	return(
		'<span><input id="' + set['id'] + '_save" class="'
		+ set['savebutton'] + '" type="button" value="ADD" /> OR '
		+ '<input id="' + set['id'] + '_cancel" class="' 
		+ set['cancelbutton'] + '" type="button" value="CANCEL" />'
		+ '<br /></span></span>'
	);
};

InPlaceNote.setEvents = function(set) {
	Event.observe(
		set['id'],
		'mouseover',
		function() { Element.addClassName(set['id'], set['css_class']); },
		false
	);
	Event.observe(
		set['id'],
		'mouseout',
		function() { Element.removeClassName(set['id'], set['css_class']); },
		false
	);
	Event.observe(
		set['id'],
		'dblclick',
		function() {
			var field = InPlaceNote.formField(set);
			var button = InPlaceNote.formButtons(set);

			new Insertion.After(set['id'], field + button);
			Field.focus(set['id'] + '_edit');
                        $(set['id']+'_editor').addClassName('editing')

			Event.observe(
				set['id'] + '_save',
				'click',
				function() { InPlaceNote.saveChanges(set); },
				false
			);

			Event.observe(
				set['id'] + '_cancel',
				'click',
				function() { InPlaceNote.cancelChanges(set); },
				false
			);

                    $(set['id'] + '_edit').select();

                    Event.observe(set['id']+'_editor', 'keydown',
                        function (evt) {

                            switch(evt.keyCode){
                                case Event.KEY_RETURN:
                                    if(evt.shiftKey)
                                        return true;
                                    InPlaceNote.saveChanges(set);
                                    break;

                                case Event.KEY_ESC:
                                    InPlaceNote.cancelChanges(set);
                                    break;
                            }
                 	})
		},
		false
	);
};

InPlaceNote.saveComplete = function(t, set) {
	Element.removeClassName(set['id'], set['css_class']);
};

InPlaceNote.saveFailed = function(t, set) {
	Element.removeClassName(set['id'], set['css_class']);
};

InPlaceNote.saveChanges = function(set) {
	var text = $F(set['id'] + '_edit');
	var addToLocation=$(set['id']);
	
	attachNote(addToLocation, text);
        flagDirty();
	
	Element.removeClassName(set['id'], set['css_class']);
        $(set['id']+'_editor').removeClassName('editing')
	Element.remove(set['id'] + '_editor');
};

InPlaceNote.cancelChanges = function(set) {
        $(set['id']+'_editor').removeClassName('editing')
	Element.remove(set['id'] + '_editor');
	Element.removeClassName(set['id'], set['css_class']);

	Element.show(set['id']);
}

InPlaceNote.makeEditable = function(args) {
	var set = InPlaceNote.settings(args);

	InPlaceNote.setEvents(set);
}

function attachNote(location, content)
{
	location.addClassName('hasNote');
	location.previous('.clicker').setAttribute('src',  sUBoatHome + 'attach.png');
	
	var buttonClicker = document.createElement('img');
	var item = document.createElement('span');
	var note = document.createElement('span');
	var newLine = document.createElement('br');
	
	buttonClicker.setAttribute('src',  sUBoatHome + 'ipnote.png');
	buttonClicker.setAttribute('class', 'clicker'); // For IE :(

	note.addClassName('review');
	note.insert(content);
	
	item.addClassName('ilnote');
	
	item.insert({top:note});
	item.insert({top:buttonClicker});
	item.insert({bottom:newLine});
	location.insert({after:item});
	
	var noteID = note.identify();
	EditInPlace.makeEditable( {
		type: 'textarea',
		id: noteID,
		save_url: 'edit.php',
		is_wrapped: true
	} );

}
